package com.yq.cp.service.basics.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.config.MyException;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.*;
import com.yq.cp.entity.permission.User;
import com.yq.cp.mapper.basics.*;
import com.yq.cp.service.basics.PatrolRouteManagementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 巡逻线路管理 服务实现类
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@Service
public class PatrolRouteManagementServiceImpl extends ServiceImpl<PatrolRouteManagementMapper, PatrolRouteManagement> implements PatrolRouteManagementService {
    @Resource
    PatrolRouteManagementMapper patrolRouteManagementMapper;
    @Resource
    InspectionRoutePointMapper inspectionRoutePointMapper;
    @Resource
    InspectionPointItemMapper inspectionPointItemMapper;
    @Resource
    InspectionPointManagementMapper inspectionPointManagementMapper;
    @Resource
    InspectionItemManagementMapper inspectionItemManagementMapper;
    @Resource
    PatrolPlanManagementMapper patrolPlanManagementMapper;
    @Override
    public void add(PatrolRouteManagement patrolRouteManagement) {
        User user = UserThreadLocal.get();
        patrolRouteManagement.setCreateName(user.getUsername());
        if (Objects.isNull(patrolRouteManagement.getId()))
            patrolRouteManagementMapper.insert(patrolRouteManagement);
        else
            patrolRouteManagementMapper.updateById(patrolRouteManagement);
        if (!patrolRouteManagement.getPointManagements().isEmpty()){
            inspectionRoutePointMapper.delete(new QueryWrapper<InspectionRoutePoint>()
                    .eq("route_id",patrolRouteManagement.getId()));
            inspectionPointItemMapper.delete(new QueryWrapper<InspectionPointItem>()
                    .eq("route_id",patrolRouteManagement.getId()));
            for (InspectionPointManagement pointManagement : patrolRouteManagement.getPointManagements()) {
                InspectionRoutePoint routePoint = new InspectionRoutePoint();
                routePoint.setRouteId(patrolRouteManagement.getId());
                routePoint.setPointId(pointManagement.getId());
                inspectionRoutePointMapper.insert(routePoint);
                if (!pointManagement.getInspectionItemManagements().isEmpty()){
//                    inspectionPointItemMapper.delete(new QueryWrapper<InspectionPointItem>()
//                            .eq("route_id",patrolRouteManagement.getId()));
                    for (InspectionItemManagement item : pointManagement.getInspectionItemManagements()) {
                        InspectionPointItem item1 = new InspectionPointItem();
                        item1.setItemId(item.getId());
                        item1.setRoutePointId(routePoint.getId());
                        item1.setRouteId(patrolRouteManagement.getId());
                        inspectionPointItemMapper.insert(item1);
                    }
                }

//                if (!pointManagement.getPointInspectionItems().isEmpty()){
//                    inspectionPointItemMapper.delete(new QueryWrapper<InspectionPointItem>()
//                            .eq("route_id",patrolRouteManagement.getId()));
//                    for (PointInspectionItems item : pointManagement.getPointInspectionItems()) {
//                        InspectionPointItem item1 = new InspectionPointItem();
//                        item1.setItemId(item.getId());
//                        item1.setRoutePointId(routePoint.getId());
//                        item1.setRouteId(patrolRouteManagement.getId());
//                        inspectionPointItemMapper.insert(item1);
//                    }
//                }

            }
        }
    }

    @Override
    public void delete(PatrolRouteManagement patrolRouteManagement) {
        List<PatrolPlanManagement> patrolPlanManagements = patrolPlanManagementMapper.selectList(new QueryWrapper<PatrolPlanManagement>()
                .eq("route_id", patrolRouteManagement.getId()));
        if (!patrolPlanManagements.isEmpty())
            throw new MyException("该路线已经关联巡检计划，请删除相关巡检计划后再试！！！");
        patrolRouteManagementMapper.deleteById(patrolRouteManagement);
        inspectionRoutePointMapper.delete(new QueryWrapper<InspectionRoutePoint>()
                .eq("route_id",patrolRouteManagement.getId()));
        inspectionPointItemMapper.delete(new QueryWrapper<InspectionPointItem>()
                .eq("route_id",patrolRouteManagement.getId()));
    }

    @Override
    public PatrolRouteManagement detail(PatrolRouteManagement vo) {
        PatrolRouteManagement byId = patrolRouteManagementMapper.selectById(vo.getId());
        List<InspectionRoutePoint> routeId = inspectionRoutePointMapper.selectList(new QueryWrapper<InspectionRoutePoint>()
                .eq("route_id", byId.getId()));
        if (!routeId.isEmpty()){
            List<InspectionPointManagement> list = inspectionPointManagementMapper.selectBatchIds(
                    routeId.stream().map(InspectionRoutePoint::getPointId).collect(Collectors.toList()));
            byId.setPointManagements(list);
            list.stream().forEach(inspectionPointManagement -> {
                InspectionRoutePoint point_id = inspectionRoutePointMapper.selectOne(new QueryWrapper<InspectionRoutePoint>()
                        .eq("point_id", inspectionPointManagement.getId())
                        .eq("route_id",vo.getId()));
                List<InspectionPointItem> pointItem = inspectionPointItemMapper.selectList(
                        new QueryWrapper<InspectionPointItem>().eq("route_point_id",
                                point_id.getId()).eq("route_id",vo.getId())
                );
                if (!pointItem.isEmpty())
                    inspectionPointManagement.setInspectionItemManagements( inspectionItemManagementMapper.selectBatchIds(pointItem.stream().map(
                            InspectionPointItem::getItemId
                    ).collect(Collectors.toList())));
            });
        }
        return byId;
    }
}
